/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Medicine;
import systole.domain.clinicalInformation.MedicinePatient;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.MedicineByPatientBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class MedicineByPatientBrokerDB extends BrokerDB implements
        MedicineByPatientBroker {

    /**
     * 
     */
    public MedicineByPatientBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicineByPatientBroker#existMedicineByPatient(systole.domain.clinicalInformation.Medicine, systole.domain.persons.Patient)
     */
    @Override
    public boolean existMedicineByPatient(Medicine medicine, Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("exist medicine by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<MedicinePatient> list = currentSession.createCriteria(MedicinePatient.class).add(Restrictions.and(Restrictions.eq("patient", patient), Restrictions.eq("medicine", medicine))).list();
            this.logger.logDebug("exist medicine by patient successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist medicine by patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe el medicamento para el paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicineByPatientBroker#getAllPatientMedicines()
     */
    @Override
    public List<MedicinePatient> getAllPatientMedicines(Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient medicine");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<MedicinePatient> list = currentSession.createCriteria(MedicinePatient.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient medicine");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient medicine, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los medicamentos del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicineByPatientBroker#insert(systole.domain.clinicalInformation.MedicinePatient)
     */
    @Override
    public void insert(MedicinePatient medicinePatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving patient medicine");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(medicinePatient);
            this.logger.logDebug("saved patient medicine");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient medicine, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicineByPatientBroker#update(systole.domain.clinicalInformation.MedicinePatient)
     */
    @Override
    public void update(MedicinePatient medicinePatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient medicine");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(medicinePatient);
            this.logger.logDebug("updated patient medicine");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient medicine, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void delete(MedicinePatient medicinePatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient medicine");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(medicinePatient);
            this.logger.logDebug("deleted patient medicine");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient medicine, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }
}
